$(function(){
	 $( "#box-tabs" ).tabs();
	 $("#rent_checkbox").click(function(){
		 if($(this).attr("checked")){
			 show_rent_checkbox();
		 }else{
			 hidden_rent_checkbox();
		 }
	 });
	 
	 $("#sell_checkbox").click(function(){
		 if($(this).attr("checked")){
			 show_sell_checkbox();
		 }else{
			 hidden_sell_checkbox();
		 }
	 });
});

function show_rent_checkbox(){
	$("#content div.box .rent_case").css("display","block");
}

function hidden_rent_checkbox(){
	$("#content div.box .rent_case").css("display","none");
}

function show_sell_checkbox(){
	$("#content div.box .sell_case").css("display","block");
}

function hidden_sell_checkbox(){
	$("#content div.box .sell_case").css("display","none");
}

function checkform(){
	var check=$(".check").get();
	var error=0;
	var ischecked=0;
	var ob_agenc="";
	$(".status").css("border","");
	$.each(check,function(key,value){
		$(this).css("border","");
		$(this).attr("title","");
		$(".tips").html("<span style='font-size:30px;color:green;'>√</span>");
		if(value["value"]===''){
			$(this).css("border","1px solid red");
			$(this).attr("title","此處必須填寫哦");
			error++;
		}else if(value["type"]==="checkbox"){
			if(value["checked"]){
				ischecked++;
			}
			if(value["id"]=='ob_agenc_1'||value["id"]=='ob_agenc_2'){
				if(value['checked']){
					ob_agenc+=value["value"];
				}
			}
		}
		
	});
	
	if(ob_agenc!=''){
		$("#ob_agenc").val(ob_agenc);
	}
	
	if(error===0&&ischecked>=1){
		$("#record").submit();
	}else{
		if(ischecked===0){
			alert("請選擇物件類別");
		}
		$(".tips").html("<span style='font-size:30px;color:red;'>注：紅色邊框的輸入框為必填項</span>");
	}
}

function c_search(){
	mydialog=art.dialog({
		title:"處理中",
		lock:true
	});
	
	var datas=new Array();
	var datas_id=new Array();
	var birth1=$("#cu_birthday").val();
	var birth2=$("#cu_birthday2").val();
	if(birth1!=''&&birth2!=''){
		$("#birth").val(birth1+','+birth2);
	}else{
		$("#birth").val('');
	}
	var data=$(".post").get();
	var i=0;
	$.each(data,function(key,value){
		if(value["type"]==='radio'){
			if(value["checked"]){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}else{
			if($(this).val()!=''&&$(this).val()!=undefined){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}
		i++;
	});
	
	$.ajax({
		type:"post",
		url:"obj_search",
		dataType:"json",
		data:{operator:"19",s_c:datas,ids:datas_id},
		success:c_search_result
	});
}

function c_search_result(data){
	if(data.status===1){
		mydialog.content("非法訪問，不能查詢").title("提示");
	}else if(data.status===0){
		mydialog.content("沒有權限，不能查詢").title("提示");
	}else if(data.status===3){
		mydialog.close();
		$("#result").html("<center>暫無查詢結果</center>");
	}else if(data.status===2){
		mydialog.close();
		var table="<table id='results'></table>";
		$("#result").html(table);
		$("#result #results").append("<thead><tr><th>所有權人</th><th>委託人</th><th>配對</th><th>操作</th><th>洽談內容</th><th>案名</th><th>建物面積</th><th>總價</th><th>車位數</th><th>委託類別</th><th>建物門牌</th><th></th></tr></thead>");
		$("#result #results").append('<tbody>');
		$.each(data.data,function(key,value){
			$("#result #results").append("<tr><td>"+value['cu_name']+"</td><td>"+value["cu_name1"]+"</td><td><a href='#'>配對</a></td><td><input type='button' value='操作' onclick=S_operator("+value['id']+",'"+value['ob_case']+"')  /></td><td>"+""+"</td><td>"+value["ob_case"]+"</td><td>"+value["de_buil"]+"</td><td>"+value["ob_total1"]+"</td><td>"+value["ob_parkuse"]+"</td><td>"+""+"</td><td>"+value["ob_add"]+"</td></tr>");
		});
		$("#result #results").append('</tbody>');
		$("#result").append("<script type='text/javascript'>$('#results th').attr('width','80');</script>");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flex.css' />");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flexigrid.pack.css' />");
		$("#result").append("<script type='text/javascript' src='../../public/js/flexigrid.pack.js'></script>");
		$("#result").append("<script type='text/javascript' src='../../public/js/flextable.js'></script>");
		//$("#result").html(data.data);
	}
}

var mydialog;
var lat;
var lng;

function showMap(){
	mydialog=art.dialog({
		title:'請稍候...',
		lock:true,
		ok:function(){ document.getElementById("ob_map").value=lat+','+lng; },
		cancel:function(){}
	});
	$.ajax({
		type:'post',
		url:'map',
		dataType:'html',
		success:showmap_status
	});
}

function showmap_status(data){
	mydialog.content(data).title("地圖定位");
	initialize();
}

function initialize() {
	var myLatlng = new google.maps.LatLng(24.997344353729364, 121.55073852549200);//默認顯示台北市
	var mapOptions = {
	    zoom: 10,
	    center: myLatlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	  
	var marker = new google.maps.Marker({
	    position: myLatlng,
	    draggable:true,
	    map: map,
	    title:"拖动来选择坐标"
	});
	  
    google.maps.event.addListener(marker, 'mouseup', function() {
		var position_str=marker.getPosition().toString();
		var str=position_str.replace("(","");
		str=str.replace(")","");
		var strs=str.split(",");
		lat=strs[0];
		lng=strs[1];
		document.getElementById("lat1").value=lat;
		document.getElementById("lng1").value=lng;
		map.setCenter(marker.getPosition());
	  });
}

function obj_check_value(obj){
	var value=$(obj).val();
	var i=value.length;
	var re=new RegExp("[0-9]{"+i+"}","g");
	if(value!=''){
		if(!re.test(value)){
			alert("這裡只能為數字哦");
			$(obj).val("");
		}
	}
}

function showAddr(id){
	showFamilyAddress(id);
}

function S_operator(c_id,c_name){
	mydialog=art.dialog({
		title:"操作-"+c_name,
		content:"<a class='operator_alter' onclick='S_alter("+c_id+");'><i class='icon-edit'></i>修改</a>&nbsp;&nbsp;<a class='operator_del' onclick='S_delete("+c_id+");' ><i class='icon-trash'></i>刪除</a>",
		lock:true
	});
	id=c_id;
}

function S_alter(id){
	//location.href="obj_alter?operator=29&&id="+id;
	window.open("obj_alter?operator=29&&id="+id,"_blank");
}

function S_delete(id){
	if(confirm("你确定删除吗？")){
		mydialog.content("...").title('处理中...');
		$.ajax({
			url:'del_action',
			type:'post',
			dataType:'json',
			data:{table:'zb052h',id:id,operator:'30'},
			success:function(data){
				if(data.status===3){
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除成功',
						lock:true,
						ok:function(){
							c_search();
					}
					});
				}else{
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除失敗',
						lock:true
					});
				}
			}
		});
	}
}

function getLineData(obj,type){
	var line=$(obj).find("option:selected").html();
	if(line!=''&&line!=undefined&&line!='&nbsp;'){
		$.ajax({
			url:'getLineData',
			type:'post',
			data:{line:line},
			dataType:'json',
			success:function(data){
				getLineData_status(data,type);
			}
		});
	}else{
		if(type==1){
			$("#station1").html('');
		}else if(type==2){
			$("#station2").html('');
		}
	}
}

function getLineData_status(data,type){
	if(data.status===1){
		if(type==1){
			var options='';
			$.each(data.data,function(key,value){
				options+="<option value='"+value['id']+"'>"+value['ba_mrt']+"</option>";
			});
			$('#station1').html(options);
		}else if(type==2){
			var options='';
			$.each(data.data,function(key,value){
				options+="<option value='"+value['id']+"'>"+value['ba_mrt']+"</option>";
			});
			$('#station2').html(options);
		}
	}else if(data.status===2){
		alert('加載捷運站失敗');
	}
}

//school start
function showSchool(id){
	mydialog=art.dialog({
		title:'選擇學校',
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'school',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}

function city_select2(obj){
	var id=$(obj).attr("value");
	var table="address_county";
	var value=$('#city option:selected').text();
	$("#address").val(value);
	if(id!=''){
		$("#school_type").val(0);
	}
}

function s_type_selected(obj){
	var value=$('#city option:selected').text()+$('#school_type option:selected').text();
	$("#address").val(value);
	var c1=$('#city option:selected').text();
	var c2=$('#school_type option:selected').text();
	if(c1!=''&&c2!=''&&c1!=undefined&&c2!=undefined){
		$("#school_name").html("");
		$.ajax({
			url:'school',
			type:'post',
			dataType:'json',
			data:{c1:c1,c2:c2,type:1},
			success:s_type_selected_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}

function s_type_selected_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['id']+"' >"+value['ba_school']+"</option>";
		});
		$("#school_name").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function school_selected(obj){
	var value=$('#school_name option:selected').text();
	$("#address").val(value);
}

// school end

/*var mydialog;
function showSchool(title,type,html_id){
	var name='zb022h';
	var fields='ba_school';
	mydialog=art.dialog({
		title:title,
		width:560,
		height:300,
		lock:true,
		content:"<iframe src='"+actionurl+"/showBaseInfo?name="+name+"&fields="+fields+"&type="+type+"' id='i_frame' height='300' width='560' ></iframe>",
		ok:function(){
			var ischecked=$(document.getElementById("i_frame").contentWindow.document).find("#show_baseinfo input:radio[name='baseinfo']:checked").val();
			if(ischecked==''||ischecked==undefined){
				alert('請選擇一筆資料');
				return false;
			}else{
				$("#"+html_id).val($(document.getElementById("i_frame").contentWindow.document).find("#"+ischecked).html());
			}
		},cancel:function(){
			
		}
	});
}*/

//park start
function showPark(id){
	mydialog=art.dialog({
		title:'選擇公園',
		width:460,
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'park',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}

function city_select_park(obj){
	var city = $('#city').val();
	if(city!='' && city!=undefined){
		$('#country').html("");
		$("#park").html("");
		$.ajax({
			url:actionurl+'/park',
			type:'post',
			dataType:'json',
			data:{city:city,type:1},
			success:city_selected_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}
function city_selected_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['ba_area']+"' >"+value['ba_area']+"</option>";
		});
		$("#country").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function country_selected_park(obj){
	var value=$('#city option:selected').text()+$('#country option:selected').text();
	$("#address").val(value);
	var c1=$('#city').val();
	var c2=$('#country').val();
	if(c1!=''&&c2!=''&&c1!=undefined&&c2!=undefined){
		$("#park").html("");
		$.ajax({
			url:actionurl+'/park',
			type:'post',
			dataType:'json',
			data:{c1:c1,c2:c2,type:2},
			success:country_selected2_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}

function country_selected2_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['id']+"' >"+value['ba_park']+"</option>";
		});
		$("#park").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function park_selected(obj){
	var value=$('#park option:selected').text();
	$("#address").val(value);
}
//park end

function show_userinfo(){
	art.dialog({
		width:500,
		height:320,
		title:'選擇客戶',
		lock:true,
		content:"<iframe src='"+actionurl+"/userinfo' class='userinfo_iframe' name='userinfo_iframe' width='500' height='320'  ></iframe>",
		ok:function(){
			var value=$(window.frames["userinfo_iframe"].document).find("#showuserinfo input:radio[name='userinfo']:checked").val();
			if(value==undefined){
				alert('請選擇客戶');
				return false;
			}else{
				var cu_name=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'1').html();
				var u_mo=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'2').html();
				var u_htel=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'3').html();
				
				$('#cu_name1').val(cu_name);
				$('#cu_mobile').val(u_mo);
				$('#cu_htel').val(u_htel);
				
				$('#cu_name2').val(cu_name);
				$('#cu_mobile1').val(u_mo);
				$('#cu_htel1').val(u_htel);
			}
		},
		cancel:function(){}
	});
}
//物件模拟配对
function peidui(title,fields){
    var demand_t = '';
	$(':checkbox:checked').each(function(i){
		   if($(this).val() == 1){
			   demand_t = demand_t + $(this).val()+',';
		   }
		   if($(this).val() == 2){
			   demand_t = demand_t + $(this).val();
		   }
	});
	ba_use = $("#ba_use").val();  	//
    de_buil1 = $("#de_buil1").val(); //
  //  de_buil2 = $("#de_buil2").val();
    ba_curre = $("#ba_curre").val();	//
    de_park1 = $("#ob_park1").val();
    de_land1 = $("#ob_stak").val();		//
    //de_land2 = $("#de_land2").val();
    ba_attac1 = $("#ba_attac").val();	//
   // ba_attac2 = $("#ba_attac2").val(); 
    ob_case1 = $("#ob_case").val();		//
    //ob_case2 = $("#ob_case2").val();
    ba_park1 = $("#ba_park1").val();	//
    ba_park2 = $("#ba_park2").val();	//
    ba_distr1 = $("#ba_distr1").val();	//
    ba_distr2 = $("#ba_distr2").val();	//
    de_admarea = $("#ba_door").val(); 	//
    //de_admarea1 = $("#de_admarea1").val();  
    de_year1 = $("#de_year").val();		//
    de_obj = $("#ob_obj").val();		//
    ba_floor = $("#ba_floor").val(); 	//
    de_floor1 = $("#ob_floor").val();	//
   // de_floor2 = $("#de_floor2").val(); 
    de_heig = $("#de_heigt").val();  	//
    ba_located1 = $("#ba_located").val(); 	//
    ba_located2 = $("#ba_located1").val();  //
    ba_styl = $("#ba_styl").val();  		//
    ba_public = $("#ba_public").val();  	//
    ba_subs = $("#ba_subs1").val(); 		// 
    ba_mana = $("#ba_mana").val(); 		// 
    de_part = $("#ba_land").val();  	//
    ba_curre = $("#ba_curre").val();  
    ba_unit = $("#ba_unit").val();		//
    
    $.ajax({
		type:"post",
		url:"pipei_search_obj",
		dataType:"json",
		data:{ba_use:ba_use,de_buil1:de_buil1,ba_curre:ba_curre,ob_park1:ob_park1,ob_stak:ob_stak,ob_stak:ob_stak,ba_attac:ba_attac,ob_case:ob_case,ba_park1:ba_park1,ba_park2:ba_park2,ba_park2:ba_park2,ba_distr1:ba_distr1,ba_distr2:ba_distr2,ba_door:ba_door,de_year:de_year,ob_obj:ob_obj,ba_floor:ba_floor,ob_floor:ob_floor,de_heigt:de_heigt,ba_located:ba_located,ba_located1:ba_located1,ba_styl:ba_styl,ba_public:ba_public,ba_public:ba_public,ba_subs1:ba_subs1,ba_mana:ba_mana,ba_land:ba_land,ba_curre:ba_curre,ba_unit:ba_unit},
		success:function(data){
			if(data.status == 3){
				alert("無匹配結果");
			}else if(data.status==2){  // 物件编号
				var table = "<input type='button' value='正式配對' onclick='zs_peidui();'/>";
				table += '<table style="border-top:1px solid #000;border-left:1px solid #000;"><tr><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件編號/<br>市調編號</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件狀態/<br>市調狀態</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">案名</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">房屋售價/<br>房屋租金</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">主建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;width:90px;">附屬建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">公設</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">車位</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">每月管理費</th>';
				$.each(data.data,function(key,value){
					table = table +'<tr><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_no"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_type1"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["cu_hbuild"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_sell1"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_mainbuil"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_subsid"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_post"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_parkspac"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_monfee"]+'</td></tr>';
					
				});
				table = table+"</tbody>";
				table = table+"</table>";
				mydialog=art.dialog({
					width:800,
					height:300,
					title:title,
					content:table				
				});
			}			
		}		
	});
	
} 